var Activity = require('../models/Activity');
var _ = require('lodash');
var ActivityUser = require('../models/ActivityUser');
var prize = require('../libs/prize');
var Prize = require('../models/Prize');

var str1 = '靠人不如靠自己!';
var str2 = ['不要轻言放弃，否则对不起自己！','不管未来的道路有远，搞机的路上有你也有我！','继续努力,胜利就在眼前!','就差最后一点点就成功了!']
var str3 = ['厉害了，Word哥！','来呀，互相伤害呀！','我来加点料！','有我在，你的人生一定会更加精彩！']

exports.activity = function (req,res){
	var CODE = req.query.code;
	res.render('activity')
}

exports.queryFromUser = function(req,res){
	var event_id = req.query.event_id?req.query.event_id:'';
	var user_id = req.query.user_id?req.query.user_id:'';

	if(event_id && user_id){
		Activity.findOne({event_id:event_id,user_id:user_id},function(err,activity){
			if(err){
				res.json({code:0,data:{},info:'发生错误'})
			}else{
				if(activity){
					res.json({code:1,data:activity,info:'返回数据'})
				}else{
					res.json({code:0,data:{},info:'查无此人'})
				}
			}
		})
	}
}

exports.queryOne = function(req,res){
	var activity_id = req.query.activity_id?req.query.activity_id:'';
	var event_id = req.query.event_id?req.query.event_id:''
	if(activity_id){
		Activity.findOne({_id:activity_id})
			.populate('user_id')
			.exec(function(err,activity){
			if(err){
				res.json({code:0,data:{},info:'发生错误'})
			}else{
				if(activity){
					ActivityUser.find({activity_id:activity._id})
						.populate('user_id')
						.exec(function(err,aus){
							if(err){

							}else{
								res.json({code:1,data:{activity:activity,helpers:aus},info:'返回数据'})	
							}
						})
				}else{
					res.json({code:0,data:{},info:'没有查询到活动信息'})
				}
				
			}
		})
	}else{
		res.json({code:0,data:{},info:'发生错误'})
	}
}

exports.create = function(req,res){
	var event_id = req.body.event_id;
	var user_id = req.body.user_id;
	console.log('create activity')
	Activity.findOne({event_id:event_id,user_id:user_id},function(err,$activity){
		if($activity){
			res.json({code:2,data:$activity,info:'砍价活动已经存在，每个人仅能创建一次活动！点击右上角按钮，分享到朋友圈让他们来帮忙吧！'})
		}else{
		var _activity = new Activity();
		_activity.event_id = event_id;
		_activity.user_id = user_id;
		_activity.create = new Date().getTime();
		_activity.save(function(err,$activity){
			if(err){
				res.json({code:0,data:{},info:'创建失败'});
			}else{
				res.json({code:1,data:$activity,info:'砍价创建成功,点击右上角按钮，分享到朋友圈让他们来帮忙吧！'})
			}
		})	
		}
	})
	
}

exports.helper = function(req,res){
	var activity_id = req.body.activity_id;
	var helper_id = req.body.helper_id;
	// console.log(activity_id,helper_id)
	var ifself;
	var help_method = parseInt(req.body.help_method);//加价

	if(activity_id && helper_id && help_method){
		Activity.findOne({_id:activity_id},function(err,activity){
			// console.log(activity)
			if(activity){
				if(activity.user_id.toString() === helper_id){
					ifself=true;
				}
				if(activity.state===1){
					ActivityUser.findOne({activity_id:activity._id,user_id:helper_id},function(err,$activityuser){
						if($activityuser){
							res.json({code:0,data:{},info:'朋友，仅有一次机会!'});
						}else{
							if(help_method===1){
								// 砍价
								cutPrice(activity.origin_price,activity.now_price,activity.dest_price,function($now,$cut){
									if($now===activity.dest_price){
										// 砍价完成，保存这次砍价记录，并将state设置成2，回传砍价完成消息
										activity.now_price = $now;
										activity.state = 2;
										var _au = new ActivityUser();
										_au.activity_id = activity._id;
										_au.user_id = helper_id;
										_au.cut = $cut;
										_au.info = str2[Math.floor(Math.random()*4)];
										_au.create_date = new Date().getTime();
										_au.help_method = 1;
										_au.save(function(err,$au){
											activity.save(function(err,$activity){
												if(err){
													res.json({code:0,data:{},info:'系统繁忙，请稍后再试!'});
												}else{
													// console.log($activity)
													prize.award($activity.user_id.toString(),function(err,$prize){
														// console.log($prize);
														if(err){
															res.json({code:0,data:{},info:'系统繁忙，请稍后再试!'});
														}else{
															res.json({code:2,data:$activity,au:$au,prize:$prize,info:'砍价完成,恭喜你获得拓攻的新年祝福!'});
														}
													})
												}
											})
										})									
									}else{
										// 保存该次砍价记录
										activity.now_price = $now;
										var _au = new ActivityUser();
										_au.activity_id = activity._id;
										_au.user_id = helper_id;
										_au.cut = $cut;
										if(ifself){
											_au.info = str1;	
										}else{
											_au.info = str2[Math.floor(Math.random()*4)];
										}
										_au.help_method = 1;
										_au.create_date = new Date().getTime();
										_au.save(function(err,$au){
											activity.save(function(err,$activity){
												if(err){
													res.json({code:0,data:{},info:'系统繁忙，请稍后再试!'});

												}else{
													res.json({code:1,data:$activity,au:$au,info:'谢谢您的帮助!'});
												}
											})
										})	

									}
								})
							}else{
								// 加价
								addPrice(activity.origin_price,activity.now_price,activity.dest_price,function($now,$cut){
																			// 保存该次砍价记录
										activity.now_price = $now;
										var _au = new ActivityUser();
										_au.activity_id = activity._id;
										_au.user_id = helper_id;
										_au.cut = $cut;
										if(ifself){
											_au.info = str1;	
										}else{
											_au.info = str3[Math.floor(Math.random()*4)];
										}
										_au.create_date = new Date().getTime();
										_au.help_method =2;
										_au.save(function(err,$au){
											activity.save(function(err,$activity){
												if(err){
													res.json({code:0,data:{},info:'系统繁忙，请稍后再试!'});

												}else{
													res.json({code:1,data:$activity,au:$au,info:'谢谢您的帮助!'});
												}
											})
										})	

								})


							}
						}
					})
				}else{
					Prize.findOne({user_id:activity.user_id},function(err,$prize){
						res.json({code:3,data:{},prize:$prize,info:'TA的砍价已经完成，谢谢您的支持！'});
					})
				}
			}
		})
}else{
	res.json({code:0,data:{},info:'砍价出错，信息不完整'});

}
	
}
 
var cutPrice = function(origin,now,dest,callback){
	if(origin===now){
		var _cut = Math.round(Math.random()*100)+200;
		callback(now-_cut,_cut)
	}else{
		var _cut = Math.round(Math.random()*50)+50;
		if(now-_cut>dest){
			callback(now-_cut,_cut);
		}else{
			callback(dest,now-dest)
		}
	}
}


var addPrice = function(origin,now,dest,callback){
	if(origin===now){
		var _cut = 0;
		callback(now,_cut)
	}else{
		var _cut = Math.round(Math.random()*10)+40;
		if(now+_cut>15792){
			callback(15792,origin-now);
		}else{
			callback(now+_cut,_cut)
		}
	}
}
